#!/bin/bash

# 容器名称
CONTAINER_NAME="secondhand-book-trading"
# 镜像名称
IMAGE_NAME="secondhand-book-trading"
# 端口映射
PORT=9090

echo "Stopping existing container..."
# 停止并删除旧容器（如果存在）
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true

echo "Starting new container with JVM optimization..."
# 运行新容器（修复语法：反斜杠后无空格，参数连续不拆分）
docker run -d \
  -p $PORT:$PORT \
  --name $CONTAINER_NAME \
  --memory=1536m \
  --memory-swap=1536m \
  -e JAVA_OPTS="-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -Xss128k -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=2 -XX:ConcGCThreads=1 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdump.hprof -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps" \
  $IMAGE_NAME java $JAVA_OPTS -jar /app.jar

echo "Container started! You can view logs with: docker logs -f $CONTAINER_NAME"